<?php
require('includes/top.php');

foreach($_FILES as $key => $value)
{
if($value['error']=='0')
{

$id = explode('userfile_',$key);
$target_path = "images/reciept/Laptop/";
$target_path = $target_path . basename($_FILES[$key]['name']); 
$date = date("d-m-y");
if(move_uploaded_file($_FILES[$key]['tmp_name'], $target_path)) {

$sql="INSERT INTO dispatched_laptop(`laptop_id`,`user_id`,`receipt`,`date`,`user_name`) VALUES ('".trim($id[1])."','".trim($id[2])."','".basename( $_FILES[$key]['name'])."','$date','".$_SESSION['user_name']."')";
$result=mysql_query($sql);
$sql1="UPDATE laptop SET `recived` = '1',`dispatch` = '1' WHERE laptop_id = '".trim($id[1])."' and user_id = '".trim($id[2])."'";
$result1=mysql_query($sql1);

header('Location:dispatch_laptop_main.php?v=success');

} else{
header('Location:dispatch_laptop_main.php?v=err');
}
}
else
{
header('Location:dispatch_laptop_main.php?v=imerr');
}
}

foreach($_POST as $key => $value)
{
$date = date("d-m-y");
$id = explode('recived_',$key);
if($id[1]!="") {
$query = mysql_query("Select recived from laptop where laptop_id = '".trim($id[1])."' and user_id = '".trim($id[2])."'");
$result_query = mysql_fetch_array($query);
	if($result_query[0]=='0');
	{
		$sql="INSERT INTO received_laptop(`laptop_id`,`date`,`user_name`,`user_id`) VALUES ('".trim($id[1])."','$date','".$_SESSION['user_name']."','".trim($id[2])."')";
		
$query = mysql_query("SELECT laptopmodel,serial_no,dispatch_location FROM laptop where user_id = '".trim($id[2])."'");
$queryresult = mysql_fetch_array($query);

$locquery = mysql_query("SELECT user_email_address FROM users where user_id = '".trim($id[2])."'");
$locqueryresult = mysql_fetch_array($locquery);



// recipients
$to  = $locqueryresult['user_email_address'];
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Laptop Details</title>
</head>
<body>
  <p><strong>Please collect the laptop from the depot</strong></p>
  <table>
    <tr>
      <td>Model No :</td><td>'.$queryresult['laptopmodel'].'</td>
    </tr>
    <tr>
      <td>Serial No :</td><td>'.$queryresult['serial_no'].'</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
	$headers .= 'From: no-reply@laptrak.com' . "\r\n";

	// Mail it
	mail($to, $subject, $message, $headers);

		$result=mysql_query($sql);
		$sql1="UPDATE laptop SET `recived` = '1' WHERE laptop_id = '".trim($id[1])."' and user_id = '".trim($id[2])."'";
		$result1=mysql_query($sql1); 
	}
}
}
header('Location:dispatch_laptop_main.php?v=success'); 
?>